﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sylvain.Manager;
using Sylvain.Model;

public partial class Admin_EditEmployeesSection : System.Web.UI.Page
{
    //Instance of the SylvainManager Class
    ISylvainManager objManger = new SylvainManager();
    /// <summary>
    /// Bind Dropdownlist with Enums
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string[] names = Enum.GetNames(typeof(TagEmployees));
            Array values = Enum.GetValues(typeof(TagEmployees));
            ddlEmployees.Items.Insert(0, "--Select Item--");
            for (int i = 0; i <= names.Length - 1; i++)
            {
                ddlEmployees.Items.Add(new ListItem(names[i], Convert.ToInt32(values.GetValue(i)).ToString()));
            }
            //Bind EmployeesSection Details with Gridview
            BindEmployeesSectionGrid();
        }
    }
    /// <summary>
    /// Bind NewsSection Details with Gridview
    /// </summary>
    protected void BindEmployeesSectionGrid()
    {

        grvEmployeesSection.DataSource = objManger.GetAllEmployeesSection();
        grvEmployeesSection.DataBind();

    }
    /// <summary>
    /// Create/Add New Record in EmployeesSection
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            EmployeesSection objEmployees = new EmployeesSection();
            if (ddlEmployees.SelectedIndex > 0 && txtTitle.Text != "" && FCKeditor.Value != "")
            {
                objEmployees.Tag = ddlEmployees.SelectedIndex;
                objEmployees.Title = txtTitle.Text.Trim();
                objEmployees.Description = FCKeditor.Value;
                objEmployees.CreatedDate = System.DateTime.Now;
                objEmployees.UpdatedDate = System.DateTime.Now;


                // insert Employees
                objManger.AddEmployeesSection(objEmployees);


                BindEmployeesSectionGrid();
                ddlEmployees.SelectedIndex = 0;
                FCKeditor.Value = "";
                txtTitle.Text = "";
                lblError.Text = "Submitted Successfully";
            }
            else
            {
                if (ddlEmployees.SelectedIndex == 0)
                {
                    lblError.Text = "Please Select an Item.";
                }
                else if (txtTitle.Text == "")
                {
                    lblError.Text = "Title is required.";
                }
                else if (FCKeditor.Value == "")
                {
                    lblError.Text = "Description is required.";
                }
                else
                {
                    lblError.Text = "";
                }
            }
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }

    }
    /// <summary>
    /// Update Record in EmployeesSection
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {

            EmployeesSection objUpdate = new EmployeesSection();
            if (ddlEmployees.SelectedIndex > 0)
            {
                objUpdate.Tag = ddlEmployees.SelectedIndex;
                objUpdate.Id = int.Parse(hidId.Value);
                objUpdate.Title = txtTitle.Text.Trim();
                objUpdate.Description = FCKeditor.Value;
                objUpdate.UpdatedDate = System.DateTime.Now;



                // Update EmployeesSection
                objManger.EditEmployeesSection(objUpdate);
                //bind Employees Section grid
                BindEmployeesSectionGrid();

                lblError.Text = "Updated Successfully";
                ddlEmployees.SelectedIndex = 0;
                FCKeditor.Value = "";
                txtTitle.Text = "";

                btnUpdate.Visible = false;
                btnSubmit.Visible = true;
            }
            else
            {
                lblError.Text = "Please Select an Item.";
            }

        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }
    }
    /// <summary>
    /// Delete/Update Record in Gridview
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployeesSection_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);

                //Delete Employees
                objManger.DeleteEmployeesSection(ID);
                BindEmployeesSectionGrid();

            }
            if (e.CommandName == "Update")
            {
                hidId.Value = e.CommandArgument.ToString();

                btnSubmit.Visible = false;
                btnUpdate.Visible = true;

                lblError.Text = "";
            }
        }
        catch
        {
            lblError.Text = "Operation could not be performed.";
        }
    }
    /// <summary>
    /// Updating a Record
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployeesSection_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)grvEmployeesSection.Rows[e.RowIndex];
            FCKeditor.Value = ((Label)row.FindControl("lblDescription")).Text;
            txtTitle.Text = ((Label)row.FindControl("lblTitle")).Text;
            ddlEmployees.SelectedIndex = Convert.ToInt32(((HiddenField)row.FindControl("hdnSelectedItem")).Value);
            btnSubmit.Visible = false;
            btnUpdate.Visible = true;
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }
    }
    /// <summary>
    /// Display Page Index
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployeesSection_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvEmployeesSection.PageIndex = e.NewPageIndex;
        BindEmployeesSectionGrid();
    }
    /// <summary>
    /// Deleting a Rocord
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvEmployeesSection_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

}